package top.hmtools.makedata.random.englishName;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 英文名称单词生成法则配置
 * @author HyboWork
 *
 */
public class WordLawConfig {
	private JSONObject config ;
	
	public WordLawConfig() {
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("dic/englishNameConfig.json");
		this.init(inputStream,"utf-8");
	}
	
	public WordLawConfig(String filePath,String charset) throws IOException {
		this.init(FileUtils.openInputStream(new File(filePath)),charset);
	}
	
	/**
	 * 初始化
	 * @param inputStream
	 * @param charset
	 */
	private void init(InputStream inputStream,String charset){
		String jsonString;
		try {
			jsonString = IOUtils.toString(inputStream, charset);
			this.config = JSON.parseObject(jsonString);
		} catch (IOException e) {
			throw new RuntimeException("初始化英文名字规则失败",e);
		}
	}
	
	/**
	 * 马尔科夫链？？
	 * @param k1
	 * @param k2
	 * @return
	 */
	public Double markov(String k1, String k2){
		if(k1 == null || k2 == null){
			return 0.0;
		}
		return this.config.getJSONObject("markov").getJSONObject(k1).getDouble(k2);
	}
	
	
	public Double maxMarkovComb() {
		return this.config.getDouble("max_markov_comb");
	}
	
	/**
	 * 发生概率
	 * @param k
	 * @return
	 */
	public Double alphaFrequency(String k) {
		return this.config.getJSONObject("alpha_freq").getDouble(k);
	}
	
	public Double dixLen() {
		return this.config.getDouble("dix_len");
	}
	
	public Double position(String k1, String k2) {
		return this.config.getJSONObject("position").getJSONObject(k1).getDouble(k2);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
